﻿
if ( typeof(controlElement.length) == "number" )
{
	controlElement = controlElement[0];
}

var xmlDocument = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
xmlDocument.async = false;
if (typeof(g_categoryVisualLevel) == "undefined")  var g_categoryVisualLevel;
if (typeof(g_categoryItemCount) == "undefined") var g_categoryItemCount;
if (typeof(g_categoryAttrib) == "undefined") var g_categoryAttrib;
var CateNodes;


if (typeof(controlElement.documentSource)!= "undefined")
{
	var fileNameIndex = controlElement.documentSource.lastIndexOf("/");

	var g_CornerPath = controlElement.documentSource.substr(0,controlElement.documentSource.lastIndexOf("/XCODE"))
		
	var structSource = g_CornerPath.substr(0, g_CornerPath.lastIndexOf("/")) + "/CATEGORY";
	structSource += controlElement.documentSource.substr(fileNameIndex);
	
	var endOfCode = controlElement.documentSource.lastIndexOf("CODE/")+5;
	
	document.write("<!--Category Index file Path :"+structSource+"-->");

	xmlDocument.load( structSource );
	xmlDocument.setProperty("SelectionNamespaces", 
	'xmlns:my="http://schema.auction.co.kr/Arche.Auction.xsd" ');
	
	var categoryIndexNode = xmlDocument.selectSingleNode("/my:CategoryIndexT");
	if (categoryIndexNode != null)
	{	
		var g_categoryParentID = categoryIndexNode.getAttribute("ParentID");
		g_categoryVisualLevel = categoryIndexNode.getAttribute("VisualLevel");
		var g_categoryID = categoryIndexNode.getAttribute("ID");
		var g_categoryAttrib = categoryIndexNode.getAttribute("AttributeType");
		
		if(g_categoryAttrib == null) g_categoryAttrib = "";
		if (g_categoryAttrib=="C")
		{
			if (typeof(g_Age)=="undefined" || g_Age ==0)
				location.href = strIAC+"/login?url=" + location.href;
			else if (g_Age<19)
				navigate(strIAC + "/buy/noadult.asp?backstep=2");
		}

		//830때 추가된 ControlType : SD 이면 소/세를 한번에 보여준다.
		
		var argListDoc = GetTransformArgumentList(controlElement.id);
		if (argListDoc.text.indexOf("SD") > -1)
		{	
			var mLevelID = categoryIndexNode.selectSingleNode("/*/my:MCategory").getAttribute("ID");
		
			var sdStructSource = g_CornerPath.substr(0, g_CornerPath.lastIndexOf("/"))
				+ "/SDCATEGORY/CATE_" + mLevelID + ".XML";
			
			document.write("<!--SD Category Index file Path :"+sdStructSource+"-->");
			
			var sdXmlDocument = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
			sdXmlDocument.async = false;
			sdXmlDocument.load( sdStructSource );
			sdXmlDocument.setProperty("SelectionNamespaces", 
			'xmlns:my="http://schema.auction.co.kr/Arche.Auction.xsd" ');
						
			var mCountSource = g_CornerPath + "/MCODE/" 
				+ controlElement.documentSource.substr( endOfCode, fileNameIndex-endOfCode+1)
				+ "CATE_" + mLevelID + ".XML";				
			
			document.write("<!--M Category Count file Path :"+ mCountSource +"-->");
			
			var mCountXmlDocument = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
			mCountXmlDocument.async = false;
			mCountXmlDocument.load( mCountSource );
			mCountXmlDocument.setProperty("SelectionNamespaces", 
			'xmlns:my="http://schema.auction.co.kr/Arche.Auction.xsd" ');			
			
			var subCategoryListNodes1 = sdXmlDocument.selectNodes("/*/my:SubCategoryList");
			var subCategoryListNodes2 = mCountXmlDocument.selectNodes("/*/my:SubCategoryList");
			
			
			if (g_categoryVisualLevel =="M")
			{
				g_categoryItemCount = mCountXmlDocument.selectSingleNode("/my:CategoryIndexT/@ItemCount").text;
			}
			else if (g_categoryVisualLevel =="D")
			{
				var sCountSource = g_CornerPath + "/SCODE/" 
					+ controlElement.documentSource.substr( endOfCode, fileNameIndex-endOfCode+1)
					+ "CATE_" + categoryIndexNode.selectSingleNode("/*/my:SCategory").getAttribute("ID") + ".XML";
			
				var sCountDocument = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
				sCountDocument.async = false;
				sCountDocument.load( sCountSource );
				sCountDocument.setProperty("SelectionNamespaces", 
				'xmlns:my="http://schema.auction.co.kr/Arche.Auction.xsd" ');
				
				var subCategoryListNodes3 = sCountDocument.selectNodes("/*/my:SubCategoryList");
				g_categoryItemCount = GetItemCount(subCategoryListNodes3);
			}			
			
			RetrieveCategoryCount(subCategoryListNodes1, subCategoryListNodes2);
			
			controlElement.xmlDocument = sdXmlDocument;
		}
		else
		{
			var dynXmlDocumentSource
				= g_CornerPath + "/"+ ((g_categoryID==g_categoryParentID)?g_categoryVisualLevel:GetParentLevel(g_categoryVisualLevel)) + "CODE/"
				+ controlElement.documentSource.substr( endOfCode, fileNameIndex-endOfCode+1)
				+ "CATE_" + g_categoryParentID + ".XML";
				
			document.write("<!--Category item count file Path :" + dynXmlDocumentSource + "-->");

			var dynXmlDocument = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
			dynXmlDocument.async = false;
			dynXmlDocument.load( dynXmlDocumentSource );
			dynXmlDocument.setProperty("SelectionNamespaces", 
			'xmlns:my="http://schema.auction.co.kr/Arche.Auction.xsd" ');
			
			var g_categoryItemCountNode
				= dynXmlDocument.selectSingleNode("/my:CategoryIndexT/@ItemCount");
			
			//현재 카테고리가 Leaf면 Count는 부모를 기준으로 가져오기 때문에 g_categoryItemCount를 자식중에 찾아야 한다.
			if (g_categoryItemCountNode!=null && g_categoryID == g_categoryParentID)
				g_categoryItemCount = g_categoryItemCountNode.value;
			else
				g_categoryItemCount = 0;
				
			var subCategoryListNodes1 = xmlDocument.selectNodes("/*/my:SubCategoryList");
			var subCategoryListNodes2 = dynXmlDocument.selectNodes("/*/my:SubCategoryList");
			
			RetrieveCategoryCount(subCategoryListNodes1, subCategoryListNodes2);
			categoryIndexNode.setAttribute("ItemCount", g_categoryItemCount);
		
			controlElement.xmlDocument = xmlDocument;
			//position.innerHTML = position.innerHTML + GetCategoryPath(xmlDocument, g_categoryVisualLevel);
		}
		
		
		CateNodes = new Array(
								xmlDocument.selectSingleNode("/*/my:LCategory"),
								xmlDocument.selectSingleNode("/*/my:MCategory"),
								xmlDocument.selectSingleNode("/*/my:SCategory"),
								xmlDocument.selectSingleNode("/*/my:DCategory"));
	}
	else
	{
		//g_categoryVisualLevel = controlElement.;
		//g_categoryItemCount = ;
	}
}
if (typeof(DrawMethodTab)!="undefined")
{
	if (typeof(SetAddMethodTab)!="undefined")
		SetAddMethodTab(g_AsrtCode, g_Kind, g_categoryAttrib);
	DrawMethodTab(g_AsrtCode);
}

function RetrieveCategoryCount(subCategoryListNodes1, subCategoryListNodes2)
{
	var count = subCategoryListNodes1.length;
	var count2 = subCategoryListNodes2.length;
	var index;
	
	if ( count== count2)
	{
		for ( index = 0; index < count; index ++ )
		{
			subCategoryListNodes1[index].setAttribute("ItemCount",
				subCategoryListNodes2[index].getAttribute("ItemCount"));
			if (subCategoryListNodes2[index].getAttribute("ID")== g_categoryID)
				g_categoryItemCount = subCategoryListNodes2[index].getAttribute("ItemCount");
		}
	}
	else
	{
		var indexDyn = 0;
		for ( index = 0; index < count; index ++ )
		{
			if (indexDyn < count2)
			{
				if (subCategoryListNodes1[index].getAttribute("ID")== subCategoryListNodes2[indexDyn].getAttribute("ID"))
				{
					subCategoryListNodes1[index].setAttribute("ItemCount",
						subCategoryListNodes2[indexDyn].getAttribute("ItemCount"));
						
					if (subCategoryListNodes2[indexDyn].getAttribute("ID")== g_categoryID)
						g_categoryItemCount = subCategoryListNodes2[indexDyn].getAttribute("ItemCount");
						
					if (subCategoryListNodes1[index].getAttribute("Name").substr(subCategoryListNodes1[index].getAttribute("Name").length-2)!="-1")
						indexDyn++;
				}
				else
					subCategoryListNodes1[index].setAttribute("ItemCount", "0");				
			}
			else
				subCategoryListNodes1[index].setAttribute("ItemCount", "0");
		}
	}
}


function GetItemCount(subCategoryListNodes)
{
	var count = subCategoryListNodes.length;
	var index;
	for ( index = 0; index < count; index ++ )
	{
		if (subCategoryListNodes[index].getAttribute("ID")== g_categoryID)
			return subCategoryListNodes[index].getAttribute("ItemCount");
	}
	return 0;
}


function GetDepth(visualLevel)
{
	return (visualLevel=="L")?0:((visualLevel=="M")?1:((visualLevel=="S")?2:3));
}
function GetParentLevel(visualLevel)
{
	var CateLevels = new Array("L", "M", "S", "D");
	var curDepth = GetDepth(visualLevel);
	if (curDepth>0)
		return CateLevels[curDepth-1];
	else
		return "H";
}

function GetCategoryPath(xmlDocument, visualLevel)
{
	var strRefURL = "";
	var strSearch = document.location.search;
	var strKind = "";
	strKind=getQueryParam("kind", strSearch);
	var strtitle = "";

	var NodeCount = GetDepth(visualLevel);
	
	var CateNodes = new Array(	xmlDocument.selectSingleNode("/*/my:MCategory"),
								xmlDocument.selectSingleNode("/*/my:SCategory"),
								xmlDocument.selectSingleNode("/*/my:DCategory"));

	if (NodeCount==0)
		strtitle += "&gt;<B><FONT color='#"+g_FontColor+"'>" + xmlDocument.selectSingleNode("/*/my:LCategory").getAttribute("Name") + "</FONT></B>";
	else
		strtitle += "&gt;<A href='/auction/category/category" + xmlDocument.selectSingleNode("/*/my:LCategory").getAttribute("ID").substring(0,2) + ".html'>"+xmlDocument.selectSingleNode("/*/my:LCategory").getAttribute("Name")+"</a>";
	
	var i;

	for (i = 0; i < NodeCount;i++)
	{
		if(i==NodeCount-1)
			strtitle += "&gt;<B><FONT color='#" +g_FontColor + "'>" + CateNodes[i].getAttribute("Name") + "</FONT></B>";
		else
			strtitle += GetNodeLink(CateNodes[i], strKind);		
	}
	return strtitle;
}

function GetNodeLink(CateNode, strKind)
{
	var strRefURL = document.location.pathname;
	strRefURL += "?asrt_code=" + CateNode.getAttribute("ID") + "&kind=" + strKind + "&flag=";
	return "&gt;<A href='" + strRefURL + "' class=link_listcolor><FONT color='#494949'>" + CateNode.getAttribute("Name") + "</FONT></A>";
}

